﻿//用户登录检测
function CheckLogin()
{
    $.ajax({
        type: "POST",
        url: "/triagecl/getUserSession",
        async: false,
        contentType: "application/text;charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.status != "true")
                window.location.href = "login.html";
        },
        error: function(){
            window.location.href = "login.html";
        }
    });
}
//获取分诊台名称
function TriageName() {
    //CheckLogin();
    $.ajax({
        type: "POST",
        async: false,
        url: "/triagecl/findByTriageName",
        dataType: "json",

        beforeSend: function () {
            $("#span_TriageName").html("数据读取中，请稍后...")
        },
        success: function (data) {
            $("#span_TriageName").html(data.return_msg)
        },
        error:function(data)
        {
            layer.msg("系统错误，请联系管理员", { shift: 1, time: 2000 });
        }
    });
}

$("#searchvalue").keypress(function () {
    if (event.keyCode == "13")
    {
        search_show();
    }
});

//获取队列列表
function ListQueueType()
{
    $.ajax({
        type: "POST",
        async: true,
        url: "/queueType/listQueueType",
        dataType: "json",

        beforeSend: function () {
            $("#li_listqueue").html("<li style='color:red'>数据读取中，请稍后...</li>")
        },
        success: function (data) {
            $("#li_listqueue").html("");
            var status = data.return_code;
            if (status == "success") {
                var count = data.count;
                if (count > 0) {
                    patientList = data.list;
                    var p = 0;
                    $("#li_listqueue").width(count * 215);
                    var html = "";
                    $.each(data.list, function (i, li) {
                        if (i == 0) {
                            if (ListNum == null)
                                ListNum = li.queue_number;
                        }
                        if (ListNum == li.queue_number)
                            p = i+1;
                        html += "<li class='queue_type_id' id='" + li.queue_number + "' onclick= 'ListPatient("
                            + li.queue_number+")"
                            + "'><div class='title'>"
                            + li.queue_name
                            + "</div><div class='left'><ul><li>等候："
                            + li.wait
                            + "</li><li>过号："
                            + li.pass_no
                            + "</li><li>已叫："
                            + li.have_station
                            + "</li></ul></div><div class='right'><ul><li>患者："
                            + li.current_visit
                            + "</li><li> </li><li>医生："
                            + li.current_doctor
                                + "</li></ul></div></li>";
                       
                    });
                    $("#li_listqueue").html(html);
                    var p_l_w = document.documentElement.clientWidth;
                    var longs = p * 215 - p_l_w;
                    if ( longs> 0)
                        $("#fzt_list").scrollLeft(longs);
                    ListPatient(ListNum);
                }
                else
                    $("#li_listqueue").html("<li style='color:red'>没有列表</li>");
            }
            else
            {
                $("#li_listqueue").html("<li style='color:red'>"+data.return_msg+"</li>");
            }
        },
        error: function () {
            $("#li_listqueue").html("<li style='color:red'>数据加载失败，请联系管理员！</li>");
        }
    });
}

//患者列表
function ListPatient(id) {
    
    $(".queue_type_id").removeClass("libg");
    ListNum = id;
    ids = "";
    $("#" + id).addClass("libg");
    var div = $("#list_" + list);
    $("#ck_" + list).prop("checked", false);
    $("#ck_" + list)[0].indeterminate = false;
    $.ajax({
        type: "POST",
        async: true,
        url: patientQueue_url,
        dataType: "json",
        data: { "queue_number": id },
        beforeSend: function () {
            div.html("<li style='color:red'>数据读取中，请稍后...</li>")
        },
        success: function (data) {
            div.html("");
            var status = data.return_code;
            if (status == "success") {
                var count = data.count;
                if (count > 0) {
                    var html = "";
                    var l_c;
                    $.each(data.list, function (i, li) {
                        html += "<div class=\"rows\"><span class=\"w10\"><input onclick=\"check_status('ck_" + list + "')\" type=\"checkbox\" id=\"" + li.id + "\" value=\"" + li.id + "\" title=\"" + li.id + "\" name=\"ck_" + list + "\" />" + li.register_id + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w15\">" + li.patient_name + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w15\">" + li.doctorName + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w10\">" + li.state_patient + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w10\">" + li.state_custom + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w10\">" + li.time_interval + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w15\">" + li.fre_date + "</span><span onclick=\"clickcheck(" + li.id + ")\" class=\"w15 span_last\">" + li.call_time + "</span></div>";
                        if (s_id != "") {
                            if (li.id == s_id)
                                l_c = i;
                        }
                    });
                    div.html(html);
                    if (s_id != "") {
                        clickcheck(s_id);
                        s_id = "";
                        if (l_c > 10)
                            div.animate({ 'scrollTop': ''+(l_c - 10) * 40+ 'px' });
                    }
                }
                else
                    div.html("<li style='color:red'>没有患者</li>");
            }
            else {
                div.html("<li style='color:red'>" + data.return_msg + "</li>");
            }
        },
        error: function () {
            div.html("<li style='color:red'>数据加载失败，请联系管理员！</li>");
        }
    });
    
}
layui.use('element', function () {
        element = layui.element();
        element.on('tab(list)', function (data) {
            //list = this.getAttribute('lay-id');
            //$.each(data, function (i, e) {
            //    list = e.val();
            //    list2 = e;
            //});
            list = "wait";
            switch (data.index) {
                case 1:
                    list = "pass";
                    break;
                case 2:
                    list = "already";
                    break;
                case 3:
                    list = "passno";
                    break;
                default:
            }
            patientQueue_url = "/patientQueue/listPatient_" + list;
            ListPatient(ListNum);
        });
    });
//全选
function check_all(name) {
    var chk = $("#" + name);
    var chks = $("input[name='" + name + "']");
    if (chk.indeterminate || chk.prop('checked')) {
        chks.each(function () {
            this.checked = true;
            
        });
    }
    else {
        chks.each(function () {
            this.checked = false;
            
        });
    }
    check_status(name);
}
//检测选择状态
function check_status(name) {
    var i = 0, j = 0;
    if (name == "ck_doctor")
        d_ids = "";
    else
        ids = "";
    var chks = $("input[name='" + name + "']");
    chks.each(function (obj) {
        i += 1;
        if ($(this).is(':checked')) {
            if (name == "ck_doctor")
                d_ids += $(this).attr("id") + ",";
            else
                ids += $(this).attr("id") + ",";
            j += 1;
            $(this).parent().parent().css("background-color", "#438eb9");
            $(this).parent().parent().css("color", "#fff");
        }
        else {
            $(this).parent().parent().css("background-color", "#fff");
            $(this).parent().parent().css("color", "#666");
        }
    });
    //ids = ids.substr(0, ids.length - 1);
    var chk = $("#" + name);
    if (i == j) {
        chk[0].indeterminate = false;
        chk.prop("checked", true);
    }
    else if (i > j && j > 0) {
        chk[0].indeterminate = true;
    }
    else {
        chk[0].indeterminate = false;
        chk.prop("checked", false);
    }
}
//点击数据行选择
function clickcheck(id)
{
    $("#" + id).trigger("click");
    s = 60;
}
//患者操作
function patientQueue(url, data) {
    if (ids != "") {
        var json = { "ids": ids, "queue_type_id": ListNum };
        if (data != null)
            json = data;
        $.ajax({
            type: "POST",
            async: true,
            url: "/patientQueue/" + url,
            dataType: "json",
            data: json,
            success: function (data) {
                if (data.return_code == "success")
                    ListQueueType();
                layer.msg(data.return_msg, { shift: 1, time: 2000 });
            },
            error: function () {
                layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
            }
        });
    }
    else {
        layer.msg("请选择要操作的患者", { shift: 1, time: 2000 });
    }
}


//延迟
function patientQueue_delay_show()
{
    layer.open(
        {
            type: 1,
            title: '延迟设置',
            skin: 'win',
            area: ['300px', '100px'],
            shade: [0.3, '#fff'],
            content: '<div class="delay">延迟时间（分钟）:<input type="button" class="w30" onclick="cutnmb(\'delay_input_time\')" value="-" /><input id="delay_input_time" type="text" value="1" class="w40" /><input type="button" class="w30" onclick="addnmb(\'delay_input_time\')" value="+"><input type="button" class="w50" id="delay_btn_sumbit" value="提交" onclick="patientQueue_delay()" /></div>',
            success: function (id, index) {
                this.enterEsc = function (event) {
                    if (event.keyCode === 13) {
                        patientQueue_delay();
                        return false;
                    }
                };
                $(document).on('keydown', this.enterEsc);
            },
            end: function () {
                ListQueueType(); 
                $(document).off('keydown', this.enterEsc); //解除键盘关闭事件
            }
        }
    );
    
}
//根据队列获取医生
function getdoclist(id)
{
    $.ajax({
        type: "POST",
        async: true,
        url: "/patientQueue/doctorId",
        dataType: "json",
        data: { "queue_type_id": id },
        success: function (data) {
            $("#r_s_doc option").remove();
            $("#r_s_doc").append("<option value='0' selected='selected'>请选择</option>");
            if (data.return_code == "success") {
                if (data.count > 0) {
                    $.each(data.list, function (i, item) {
                        $("#r_s_doc").append("<option value='" + item.id + "'>" + item.name + "</option>");
                    });
                }
            }
        }
    });
}
//转诊
function patientQueue_referralQueueType_show() {
    var s_dl = "<div class='left'>选择队列:</div><div class='right'><select id=\'r_s_dl\' style='width:150px;overflow:hidden' onchange='getdoclist(this.value)'><option value='0' selected='selected'>请选择</option>";
    $.each(patientList, function (i, item) {
        if (item.queue_number != ListNum)
            s_dl += "<option value='" + item.queue_number + "'>" + item.queue_name + "</option>";
    });
    s_dl += "</select></div><div class='left'>叫号器/医生:</div><div class='right'><select id=\'r_s_doc\' style='width:150px;overflow:hidden'></select></div><div class='left'>&nbsp;</div><div class='right'><input type='button' class='w50'  value='提交' onclick='referralQueueType()' /></div>";
    layer.open(
        {
            type: 1,
            title: '转诊设置',
            skin: 'win',
            area: ['300px', '160px'],
            shade: [0.3, '#fff'],
            content: "<div class='Manual'>"+s_dl+"</div>",
            success: function (id, index) {
                $("#r_s_doc option").empty();
                $("#r_s_doc").append("<option value='0' selected='selected'>请选择</option>");
                $.ajax({
                    type: "POST",
                    async: true,
                    url: "/patientQueue/doctorId",
                    dataType: "json",
                    data: { "queue_type_id": $("#r_s_dl").val() },
                    success: function (data) {
                        if (data.return_code == "success") {
                            if (data.count > 0) {
                                $.each(data.list, function (i, item) {
                                    $("#r_s_doc").append("<option value='" + item.id + "'>" + item.name + "</option>");
                                });
                            }
                        }
                    }
                });

                this.enterEsc = function (event) {
                    if (event.keyCode === 13) {
                        referralQueueType();
                        return false;
                    }
                };
                $(document).on('keydown', this.enterEsc);
            },
            end: function () {
                $(document).off('keydown', this.enterEsc); //解除键盘关闭事件
            }
        }
    );
}
//分诊
function examination_show() {
    $.ajax({
        type: "POST",
        async: true,
        url: "/patientQueue/doctorId",
        dataType: "json",
        data: {"queue_type_id":ListNum},
        success: function (data) {
            if (data.return_code == "success") {
                var s_html = "<select id=\'examination_select\' style='width:150px;overflow:hidden'><option value='0'>请选择</option>";
                $.each(data.list, function (i, item) {
                    //if (item.queue_number != ListNum)
                    s_html += "<option value='" + item.id + "'>" + item.name + "</option>";
                });
                s_html += "</select>";
                layer.open(
                    {
                        type: 1,
                        title: '分诊设置',
                        skin: 'win',
                        area: ['330px', '100px'],
                        shade: [0.3, '#fff'],
                        content: '<div class="delay">医生/叫号器:' + s_html + '<input type="button" class="w50"  value="提交" onclick="examination()" /></div>',
                        success: function (id, index) {
                            this.enterEsc = function (event) {
                                if (event.keyCode === 13) {
                                    examination();
                                    return false;
                                }
                            };
                            $(document).on('keydown', this.enterEsc);
                        },
                        end: function () {
                            $(document).off('keydown', this.enterEsc); //解除键盘关闭事件
                        }
                    }
                );
            }

        },
        error: function () {
            layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
        }
    });

}
//手动录入
function Manual_show() {

    var s_html = "<select id=\'manual_select\' style='width:150px;overflow:hidden'>";
    $.each(patientList, function (i, item) {
        //if (item.queue_number != ListNum)
        s_html += "<option value='" + item.queue_number + "'>" + item.queue_name + "</option>";
    });
    s_html += "</select>";
    layer.open(
        {
            type: 1,
            title: '手动录入',
            skin: 'win',
            area: ['300px', '160px'],
            shade: [0.3, '#fff'],
            content: '<div class="Manual"><div class="left">患者姓名:</div><div class="right"><input type="text" id="patientName" /></div><div class="left">队列:</div><div class="right">' + s_html + '</div><div class="left">&nbsp;</div><div class="right"><input type="button" class="w50"   value="提交" onclick="Manual()" /></div></div>',
            success: function (id, index) {
                this.enterEsc = function (event) {
                    if (event.keyCode === 13) {
                        Manual();
                        return false;
                    }
                };
                $(document).on('keydown', this.enterEsc);
            },
            end: function () {
                ListQueueType();
                $(document).off('keydown', this.enterEsc); //解除键盘关闭事件
            }
        }
    );
}
function search() {
    var index = layer.index;
    layer.close(index);};
//搜索
function search_show()
{
    if ($("#searchvalue").val() != "") {
       
        $.ajax({
            type: "POST",
            async: true,
            url: "/patientQueue/ScanAdd",
            dataType: "json",
            data: { "code": $("#searchvalue").val()},
            success: function (data) {
                if (data.return_code == "success") {
                    
                    s_id = data.id;
                    ids = data.id;
                    var p_s = data.state_patient;
                    var status = "wait";
                    if (p_s == 1)
                        status = "passno";
                    else if (p_s == 53)
                        status = "already";
                    else if (p_s == 54)
                        status = "pass";
                    ListNum = data.queue_type_id;
                    patientQueue_url = "/patientQueue/listPatient_" + status;
                    element.tabChange('list', status);
                    //ListQueueType();
                    //ListPatient(ListNum);
                }
                layer.msg(data.return_msg, { shift: 1, time: 2000 });
            },
            error: function () {
                layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
            }
        });

    }
    else
        layer.msg("请输入查询号", { shift: 1, time: 2000 });
}
function cutnmb(id) {
    var num = $("#" + id);
    var n = parseInt(num.val());
    if (n > 1)
        num.val(n - 1);
}
function addnmb(id) {
    var num = $("#" + id);
    var n = parseInt(num.val());
    num.val(n + 1);
}

function patientQueue_delay() {
    var data = { "ids": ids, "queue_type_id": ListNum, "timeInterval": $("#delay_input_time").val() };
    var index = layer.index;
    layer.close(index);
    patientQueue("delay", data);
    
}
function referralQueueType() {
    if ($("#r_s_doc").val() != 0 && $("#r_s_dl").val() != 0) {
        var data = { "ids": ids, "queue_type_id": $("#r_s_dl").val(), "id": $("#r_s_doc").val() };
        ListNum = $("#r_s_dl").val();
        var index = layer.index;
        layer.close(index);
        patientQueue("referralQueueType", data);
    }
    else
        layer.msg("请选择队列或医生", { shift: 1, time: 2000 });
}

function examination()
{
    if ($("#examination_select").val() != 0) {
        var data = { "ids": ids, "queue_type_id": ListNum, "id": $("#examination_select").val() };
        var index = layer.index;
        layer.close(index);
        patientQueue("examination", data);
    }
    else
        layer.msg("请选择叫号器或医生", { shift: 1, time: 2000 });
}
function examination_cannel() {
    var data = { "ids": ids};
    //var index = layer.index;
    //layer.close(index);
    patientQueue("noexamination", data);
}

function Manual() {
    var name = $("#patientName").val();
    var id = $("#manual_select").val();
    var json = { "patientName": name, "QueueNumber": id };
    
    if (name != "" && id != "") {
        var index = layer.index;
        layer.close(index);
        $.ajax({
            type: "POST",
            async: true,
            url: "/patientQueue/Manual",
            dataType: "json",
            data: json,
            success: function (data) {
                if (data.return_code == "success") {
                    ListNum = id;
                    ListQueueType();
                }
                layer.msg(data.return_msg, { shift: 1, time: 2000 });
            },
            error: function () {
                layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
            }
        });
    }
    else
    {
        layer.msg("患者姓名和队列不能为空", { shift: 1, time: 2000 });
    }
}

//医生排班
function getRltDoctor() {
    $.ajax({
        type: "POST",
        async: true,
        url: "/paiban/DoctorOrPagerPaiban",
        dataType: "json",
        success: function (data) {
            if (data.return_code == "success") {
                $("#list_rltdoc").html();
                var html = "";
                var l_d = 0;
                $.each(data.list, function (i, item) {
                    html += "<div class=\"rows\" ><span class=\"w5\"><input onclick=\"check_status('ck_doctor')\" type=\"checkbox\" id=\"d_" + item.id + "\" value=\"" + item.queue_type_id + "\" title=\"" + item.id + "\" name=\"ck_doctor\" />" + item.id + "</span><span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w10\" title=\"" + item.queueTypeName + "\">" + item.queueTypeName + "</span><span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w10\"  title=\"" + item.doctorNmae + "\">" + item.doctorNmae + "</span>";
                    var d = item.onduty.split("");
                    $.each(d, function (j, li_d) {
                        switch (li_d) {
                            case '0':
                                html += "<span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w10\">休息</span>";
                                break;
                            case '1':
                                html += "<span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w10\">全天</span>";
                                break;
                            case '2':
                                html += "<span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w5\">上午</span><span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w5\" class=\"w5\">&nbsp;</span>";
                                break;
                            case '3':
                                html += "<span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w5\">&nbsp;</span><span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w5\" class=\"w5\">下午</span>";
                                break;
                            default:
                                html += "<span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w5\">&nbsp;</span><span onclick=\"clickcheck('d_" + item.id + "')\" class=\"w5\" class=\"w5\">&nbsp;</span>";
                                break;
                        }
                    });
                    html += "<span class=\"w5\"><i class=\"layui-icon\" style='cursor:pointer;' onclick=\"doc_edit(" + item.id + ",'" + item.onduty + "','id','" + item.queueTypeName + "-" + item.doctorNmae + "')\">&#xe642;</i>&nbsp;&nbsp;<i class=\"layui-icon\"  style='cursor:pointer;'  onclick=\"doc_del(" + item.id + ")\">&#xe640;</i>&nbsp;</span></div>";
                    if (d_ids == item.id)
                        l_d = i;
                });
                $("#list_rltdoc").html(html);
                if (d_ids != "") {
                    var s_d_id = d_ids.substr(0, d_ids.length - 1);
                    var l_d_id = s_d_id.split(",");
                    $.each(l_d_id, function (i, id) {
                        clickcheck(id);
                    });
                }
                if (l_d > 10)
                    $("#list_rltdoc").animate({ 'scrollTop': '' + (l_d - 10) * 40 + 'px' });
            }
            //layer.msg(data.return_msg, { shift: 1, time: 2000 });
        }
    });
}
function del_confirm() {
    var rr = false;
    
    return rr;

    
}
function doc_del(id) {
    if (id != "")
        d_ids = "d_" + id + ",";
    if (d_ids != "") {
        layer.confirm('确实要删除吗？', {
            btn: ['确定', '取消'], //按钮
            shade: [0.5, '#fff'] //显示遮罩
        }, function (index) {

            $.ajax({
                type: "POST",
                async: true,
                url: "/paiban/delete_pb",
                dataType: "json",
                data: { "ids": d_ids },
                success: function (data) {
                    if (data.return_code == "success") {
                        d_ids = "";
                        getRltDoctor();
                    }
                    layer.msg(data.return_msg, { shift: 1, time: 2000 });
                },
                error: function () {
                    layer.msg("操作失败，请联系管理员 ", { shift: 1, time: 2000 });
                }
            });
            layer.close(index);
        });
    }
    else
        layer.msg("请选择要删除的排班信息！", { shift: 1, time: 2000 });
}
//医生排班信息修改

function doc_edit(id,onduty,ids,doc)
{
    if (id != "")
        d_ids = "d_"+id+",";
    layer.open(
        {
            type: 1,
            title: '医生排班信息修改',
            skin: 'win',
            area: ['400px', '400px'],
            shade: [0.3, '#fff'],
            content: '<div class="Paiban"><div class="left">&nbsp;</div><div class="right">' + doc + '</div><div class="left">周一:</div><div class="right"><input type="radio"name="0"value="0"/>休息<input type="radio"name="0"value="1"/>全天<input type="radio"name="0"value="2"/>上午<input type="radio"name="0"value="3"/>下午</div><div class="left">周二:</div><div class="right"><input type="radio"name="1"value="0"/>休息<input type="radio"name="1"value="1"/>全天<input type="radio"name="1"value="2"/>上午<input type="radio"name="1"value="3"/>下午</div><div class="left">周三:</div><div class="right"><input type="radio"name="2"value="0"/>休息<input type="radio"name="2"value="1"/>全天<input type="radio"name="2"value="2"/>上午<input type="radio"name="2"value="3"/>下午</div><div class="left">周四:</div><div class="right"><input type="radio"name="3"value="0"/>休息<input type="radio"name="3"value="1"/>全天<input type="radio"name="3"value="2"/>上午<input type="radio"name="3"value="3"/>下午</div><div class="left">周五:</div><div class="right"><input type="radio"name="4"value="0"/>休息<input type="radio"name="4"value="1"/>全天<input type="radio"name="4"value="2"/>上午<input type="radio"name="4"value="3"/>下午</div><div class="left">周六:</div><div class="right"><input type="radio"name="5"value="0"/>休息<input type="radio"name="5"value="1"/>全天<input type="radio"name="5"value="2"/>上午<input type="radio"name="5"value="3"/>下午</div><div class="left">周日:</div><div class="right"><input type="radio"name="6"value="0"/>休息<input type="radio"name="6"value="1"/>全天<input type="radio"name="6"value="2"/>上午<input type="radio"name="6"value="3"/>下午</div><div class="left">&nbsp;</div><div class="right"><input type="button"class="w50"value="提交"onclick="doc_edit_add()"/></div></div>',
            success: function (id, index) {
                this.enterEsc = function (event) {
                    if (event.keyCode === 13) {
                        doc_edit_add();
                        return false;
                    }
                };
                $(document).on('keydown', this.enterEsc);
                if (onduty != "") {
                    var d = onduty.split("");
                    $.each(d, function (i, li_d) {
                        $(":radio[name='" + i + "'][value='" + li_d + "']").prop("checked", "checked");
                    });
                }
            },
            end: function () {
                $(document).off('keydown', this.enterEsc); //解除键盘关闭事件
            }
        }
    );
}



function doc_edit_add()
{
    if (d_ids != "") {
        var f_onduty = false;
        var onduty = "";
        for (var i = 0; i < 7; i++) {
            //$("input[name='rd']:checked").val();
            var s = $(":radio[name='" + i + "']:checked").val();
            if (s != null && s != "0") {
                onduty += s;
                f_onduty = true;
            }
            else
                onduty += "0";
        }
        if (f_onduty) {
            $.ajax({
                type: "POST",
                async: true,
                url: "/paiban/update_pb",
                dataType: "json",
                data: { "ids": d_ids, "onduty": onduty },
                success: function (data) {
                    if (data.return_code == "success") {
                        var index = layer.index;
                        layer.close(index);
                        getRltDoctor();
                    }
                    layer.msg(data.return_msg, { shift: 1, time: 2000 });
                },
                error: function () {
                    layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
                }
            });
        }
        else
            layer.msg("至少选择一个非休息排班信息", { shift: 1, time: 2000 });
    }
    else
        layer.msg("请选择要操作的医生", { shift: 1, time: 2000 });
}


function doc_add() {
    var s_html = "<select id=\'d_manual_select\' style='width:150px;overflow:hidden'><option value='0' selected='selected'>请选择</option>";
    $.each(patientList, function (i, item) {
        //if (item.queue_number != ListNum)
        s_html += "<option value='" + item.queue_number + "'>" + item.queue_name + "</option>";
    });
    s_html += "</select>";
    layer.open(
        {
            type: 1,
            title: '医生排班信息添加',
            skin: 'win',
            area: ['400px', '520px'],
            shade: [0.3, '#fff'],
            content: '<div class="Paiban"><div class="left">选择队列:</div><div class="right">' + s_html + '</div><div class="left">选择医生:</div><div class="right"><select id="pb_doc" style="width: 150px;overflow: hidden"></select></div><div class="left">周一:</div><div class="right"><input type="radio"name="0"value="0"/>休息<input type="radio"name="0"value="1"/>全天<input type="radio"name="0"value="2"/>上午<input type="radio"name="0"value="3"/>下午</div><div class="left">周二:</div><div class="right"><input type="radio"name="1"value="0"/>休息<input type="radio"name="1"value="1"/>全天<input type="radio"name="1"value="2"/>上午<input type="radio"name="1"value="3"/>下午</div><div class="left">周三:</div><div class="right"><input type="radio"name="2"value="0"/>休息<input type="radio"name="2"value="1"/>全天<input type="radio"name="2"value="2"/>上午<input type="radio"name="2"value="3"/>下午</div><div class="left">周四:</div><div class="right"><input type="radio"name="3"value="0"/>休息<input type="radio"name="3"value="1"/>全天<input type="radio"name="3"value="2"/>上午<input type="radio"name="3"value="3"/>下午</div><div class="left">周五:</div><div class="right"><input type="radio"name="4"value="0"/>休息<input type="radio"name="4"value="1"/>全天<input type="radio"name="4"value="2"/>上午<input type="radio"name="4"value="3"/>下午</div><div class="left">周六:</div><div class="right"><input type="radio"name="5"value="0"/>休息<input type="radio"name="5"value="1"/>全天<input type="radio"name="5"value="2"/>上午<input type="radio"name="5"value="3"/>下午</div><div class="left">周日:</div><div class="right"><input type="radio"name="6"value="0"/>休息<input type="radio"name="6"value="1"/>全天<input type="radio"name="6"value="2"/>上午<input type="radio"name="6"value="3"/>下午</div><div class="left">&nbsp;</div><div class="right"><input type="button"class="w50"value="提交"onclick="doc_add_save()"/></div></div>',
            success: function (id, index) {
                this.enterEsc = function (event) {
                    if (event.keyCode === 13) {
                        doc_add_save();
                        return false;
                    }
                };
                var onduty = "1111111";
                var d = onduty.split("");
                $.each(d, function (i, li_d) {
                    $(":radio[name='" + i + "'][value='" + li_d + "']").prop("checked", "checked");
                });
                $(document).on('keydown', this.enterEsc);
                $("#pb_doc option").empty();
                $("#pb_doc").append("<option value='0' selected='selected'>请选择</option>");
                $.ajax({
                    type: "POST",
                    async: true,
                    url: "/doctor/list_doctor",
                    dataType: "json",
                    data: { "queue_type_id": ListNum },
                    success: function (data) {
                        if (data.return_code == "success") {
                            if (data.count > 0) {
                                $.each(data.list, function (i, item) {
                                    $("#pb_doc").append("<option value='" + item.doctor_id + "'>" + item.name + "</option>");
                                });
                            }
                        }
                    }
                });
            },
            end: function () {
                $(document).off('keydown', this.enterEsc); //解除键盘关闭事件
            }
        }
    );
}

function doc_add_save()
{
    var doctor_id = $("#pb_doc").val();
    var queue_type_id = $("#d_manual_select").val();
    var f_onduty = false;
    var onduty = "";
    for (var i = 0; i < 7; i++) {
        //$("input[name='rd']:checked").val();
        var s = $(":radio[name='" + i + "']:checked").val();
        if (s != null && s != "0") {
            onduty += s;
            f_onduty = true;
        }
        else
            onduty += "0";
    }
    if (doctor_id != "0" && queue_type_id != "0") {
        if (f_onduty) {
            var index = layer.index;
            layer.close(index);
            $.ajax({
                type: "POST",
                async: true,
                url: "/paiban/DoctorOrPagerPaibanAdd",
                dataType: "json",
                data: {"doctor_id": doctor_id, "queue_type_id": queue_type_id, "onduty": onduty},
                success: function (data) {
                    if (data.return_code == "success") {
                        var index = layer.index;
                        layer.close(index);
                        getRltDoctor();
                    }
                    layer.msg(data.return_msg, { shift: 1, time: 2000 });
                },
                error: function () {
                    layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
                }
            });
        }
        else
            layer.msg("至少选择一个非休息排班信息", { shift: 1, time: 2000 });
    }
    else {
        layer.msg("请选择医生和队列", { shift: 1, time: 2000 });
    }
}


//读取医生已叫患者
function getDocCaller() {
    $("#call_doc_count").html("0");
    $.ajax({
        type: "POST",
        async: true,
        url: "/qmj/list_content_patient",
        dataType: "json",
        success: function (data) {
            $("#call_list").html("");
            var html = "<div class=\"rows\"><span class=\"w15\">序号</span><span class=\"w20\">科室</span><span class=\"w20\">医生</span><span class=\"w20\">患者</span><span class=\"w25\">操作</span></div>";
            var html2 = "";
            if (data.return_code == "success") {
                $("#call_doc_count").html(data.count);
                if (data.count > 0)
                {
                    $.each(data.list, function (i, item) {
                        html += "<div class=\"rows\"><span class=\"w15\">" + item.register_id + "</span><span class=\"w20\">" + item.queuetypename + "</span><span class=\"w20\">" + item.doctorname + "</span><span class=\"w20\">" + item.patient_name + "</span><span class=\"w25\"><a href=\"javascript:fztCaller(" + item.queue_type_id + "," + item.id + ")\">呼叫</a>&nbsp;<a href=\"javascript:patientQueue('newlyDiagnosed',{\"ids\":" + item.id + "})\">取消</a></span></div>";
                        if (item.istype==1)
                            html2 += "<div class=\"rows2\">" + item.queuetypename + "诊室" + item.doctorname + "医生呼叫" + item.patient_name + "患者</div>";
                    });
                    $("#call_list").html(html);
                    if (html2 != "") {
                        $("#doc_call_rows").html(html2);
                        $("#show_tips").animate({ bottom: '0px' }, 1000).delay(3000).animate({ bottom: '-160px' }, 1000);
                    }
                }
            }
        },
        error: function () {
            layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
        }
    });
}

function fztCaller(q_id, p_id)
{
    $.ajax({
        type: "POST",
        async: true,
        url: "/qmj/update_content",
        dataType: "json",
        data: { "queue_type_id": q_id, "id": p_id},
        success: function (data) {
            getDocCaller();
            layer.msg("呼叫成功", { shift: 1, time: 2000 });
        },
        error: function () {
            layer.msg("操作失败，请联系管理员", { shift: 1, time: 2000 });
        }
    });
}


function call_doc_close() {

    if ($("#call_list").is(":hidden")) {
        $("#call_doc_count_btn").html("&or;")
        $("#call_list").show();
        getDocCaller();
    } else {
        $("#call_doc_count_btn").html("&and;")
        $("#call_list").hide();
    }
}

